<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>专业管理</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/layui/layui.js"></script>
</head>
<body>
<!--添加功能的form-->
<form class="layui-form" action="" id="insertspecialityForm" lay-filter="insertspecialityForm"
      style="display: none;margin-right: 40px;margin-top: 20px">
    <div class="layui-form-item" style="display: none">
        <label class="layui-form-label">id</label>
        <div class="layui-input-inline">
            <input type="text" name="id"   autocomplete="off"
                   class="layui-input" >
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">专业名称</label>
        <div class="layui-input-block">
            <input type="text" name="specialityName" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">层次</label>
        <div class="layui-input-block">
            <input type="text" name="degree" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">学习年限</label>
        <div class="layui-input-block">
            <input type="number" name="studyTime" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">总学费</label>
        <div class="layui-input-block">
            <input type="number" name="totleTuition" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">学分</label>
        <div class="layui-input-block">
            <input type="number" name="credit" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">入学考试科目</label>
        <div class="layui-input-block">
            <input type="text" name="subject" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit lay-filter="insertspecialityFilter">立即提交</button>
        </div>
    </div>
</form>

<!--编辑功能的form-->
<form class="layui-form" action="" id="updatespecialityForm" lay-filter="updatespecialityForm"
      style="display: none;margin-right: 40px;margin-top: 20px">

    <input type="hidden" name="id">
    <div class="layui-form-item">
        <label class="layui-form-label">专业名称</label>
        <div class="layui-input-block">
            <input type="text" name="specialityName" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">层次</label>
        <div class="layui-input-block">
            <input type="text" name="degree" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">学习年限</label>
        <div class="layui-input-block">
            <input type="number" name="studyTime" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">总学费</label>
        <div class="layui-input-block">
            <input type="number" name="totleTuition" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">学分</label>
        <div class="layui-input-block">
            <input type="number" name="credit" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">入学考试科目</label>
        <div class="layui-input-block">
            <input type="text" name="subject" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="updatespecialityFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<!--顶部搜索框-->
<form class="layui-form" action="" id="search"style="margin-top: 10px" lay-filter="searchspecialityFormFilter">
    <div class="layui-inline">
        <label class="layui-form-label">专业名称:</label>
        <div class="layui-input-inline">
            <input type="text" name="specialityName" autocomplete="off" placeholder="专业名称" class="layui-input">
        </div>
    </div>


    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="searchFilter">搜索</button>
            <button type="reset" class="layui-btn layui-btn-primary" id="myButton">重置</button>
        </div>
    </div>
</form>
<table id="showspeciality" lay-filter="specialityFilter"></table>
</body>
<!--顶部工具栏-->
<script type="text/html" id="specialityToolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-xs" lay-event="addspeciality">
            <i class="layui-icon layui-icon-add-1"></i>添加
        </button>
        <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="deletespecialitys">
            <i class="layui-icon layui-icon-delete"></i>删除
        </button>
    </div>
</script>
<!--实现部门管理操作功能按钮-->
<script type="text/html" id="specialityBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!--状态按钮-->

<script>
    layui.use(['table', 'layer', 'jquery', 'form'], function () {
        let table = layui.table;
        let layer = layui.layer;
        let $ = layui.jquery;
        let form = layui.form;

        let specialityTable = table.render({
            elem: '#showspeciality'
            , url: '/speciality/selectAll' //数据接口
            , page: true//开启分页
            , limit: 5
            , limits: [5, 10, 15, 20]
            , defaultToolbar: ['filter', 'exports', 'print']
            , toolbar: '#specialityToolbar' //开启头部工具栏，并为其绑定左侧模板

            , title: 'specialityTable'
            , cols: [
                [ //表头
                    {type: 'checkbox', fixed: 'left'}
                    , {field: 'id', title: '编号', width: 80, sort: true, fixed: true}
                    , {field: 'specialityName', title: '专业名称', width: 100}
                    , {field: 'degree', title: '层次', width: 110, sort: true}
                    , {field: 'studyTime', title: '学习年限', width: 110, sort: true}
                    , {field: ' totleTuition', title: '总学费', width: 110, sort: true}
                    , {field: 'credit', title: '学分', width: 110, sort: true}
                    , {field: 'subject', title: '入学考试科目', width: 110, sort: true}

                    , {fixed: 'right', align: 'center', title: '操作', toolbar: '#specialityBar', width: 120}
                ]
            ]
        });
        //监听行工具事件
        table.on('tool(specialityFilter)', function (obj) {
            let data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    //删除数据库中的数据
                    let idList=[];
                    //遍历数组data，抽取所有的id
                    idList.push(data.id);
                    $.ajax({
                        url: "/speciality/delete",
                        type: "DELETE",
                        dataType: "JSON",
                        data: {idList:idList},
                        success: function (ret) {
                            if (ret.code == 0) {
                                //刷新表格数据
                                specialityTable.reload({
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                });
                                layer.msg(ret.msg, {icon: 6});
                            } else {
                                layer.msg(ret.msg, {icon: 5});
                            }
                        }
                    });
                    layer.close(index);
                    //向服务端发送删除指令
                });
            } else if (layEvent === 'edit') {

                form.val('updatespecialityForm', {
                    'id': data.id,
                    'specialityName': data.specialityName,
                    'degree': data.degree,
                    'studyTime': data.studyTime,
                    'totleTuition': data.totleTuition,
                    'credit': data.credit,
                    'subject': data.subject,


                });
                layer.open({
                    title: '编辑部门',
                    type: 1,
                    area: ['500px', '500px'],
                    content: $("#updatespecialityForm")//这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });
            }
        });

        //form表单实现更新功能
        form.on('submit(updatespecialityFilter)', function (data) {

            //提交表单数据
            $.ajax({
                url: "/speciality/update",
                type: "PUT",
                dataType: "JSON",
                contentType:"application/json;charset=UTF-8",
                data:JSON.stringify(data.field),
                success: function (ret) {
                    if (ret.code == 0) {
                        //关闭对话框
                        layer.closeAll();
                        //刷新表格数据
                        specialityTable.reload({
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        });
                        layer.msg(ret.msg, {icon: 6});
                    } else {
                        layer.msg(ret.msg, {icon: 5});
                    }
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //监听头工具栏事件
        table.on('toolbar(specialityFilter)', function (obj) {
            let checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据
            switch (obj.event) {
                case 'addspeciality':
                    document.getElementById("insertspecialityForm").reset();
                    layer.open({
                        title: '添加部门',
                        type: 1,
                        area: ['500px', '450px'],
                        content: $("#insertspecialityForm")
                    });
                    break;
                case 'deletespecialitys':
                    if (data.length === 0) {
                        layer.msg("请选择一行数据");
                    } else {
                        let idList = [];
                        for (let i = 0; i < data.length; i++) {
                            idList[i] = data[i].id;
                        }
                        layer.confirm('真的删除吗', function (index) {
                            $.ajax({
                                url: "/speciality/delete",
                                type: "delete",
                                data: {idList: idList},
                                dataType: "json",
                                success: function (result) {
                                    if (result.code == '0') {
                                        layer.closeAll();
                                        specialityTable.reload({
                                            page: {
                                                curr: 1 //重新从第 1 页开始
                                            }
                                        });
                                        layer.msg(result.msg, {icon: 6});
                                    } else {
                                        layer.msg(result.msg, {icon: 5});
                                    }
                                }
                            })
                        })
                    }
                    break;
            }
        });

        //form表单实现添加功能
        form.on('submit(insertspecialityFilter)', function (data) {
            $("#insertspecialityButton").attr("class","layui-btn layui-btn-disabled")

            //提交表单数据

                $.ajax({
                    url: "/speciality/insert",
                    type: "POST",
                    dataType: "JSON",
                    contentType:"application/json;charset=UTF-8",
                    data:JSON.stringify(data.field),
                    success: function (ret) {
                        if (ret.code == 0) {
                            //关闭对话框
                            layer.closeAll();
                            //刷新表格数据
                            specialityTable.reload({
                                page: {
                                    curr: 1 //重新从第 1 页开始
                                }
                            });
                            layer.msg(ret.msg, {icon: 6});
                        } else {
                            layer.msg(ret.msg, {icon: 5});
                        }
                    }
                });


            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //实现顶部搜索框
        form.on('submit(searchFilter)', function (data) {
            console.log(data.field)
                        //刷新表格数据
                        specialityTable.reload({
                            where: { //设定异步数据接口的额外参数，任意设
                                specialityName: data.field.specialityName}
                                ,
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        });




            return false;
        });
        //重置条件查询表单，刷新table数据
        $("#myButton").click(function () {
            specialityTable.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    specialityName: "",

                }   ,
                page: {
                    curr: 1 //重新从第 1 页开始
                }

            });
        });
    });
</script>
</html>